﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using Core.Mvvm.Commands;

namespace Core.Mvvm.ViewModels
{
    public class ClosableViewModel : BaseViewModel, IClosableViewModel
    {
        public ClosableViewModel(Dispatcher dispatcher) : base(dispatcher)
        {
        }

        public event EventHandler RequestClose;

        private ICommand _closeCommand;

        public ICommand CloseCommand
        {
            get { return _closeCommand ?? (_closeCommand = new SimpleCommand(OnClose, CanClose)); }
        }

        protected virtual void OnClose()
        {
            OnRequestClose();
        }

        protected virtual bool CanClose()
        {
            return true;
        }

        protected virtual void OnRequestClose()
        {
            var handler = RequestClose;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
}
